package com.bbzn.device.client.service.impl.cwting;

import com.alibaba.fastjson.JSON;
import com.bbzn.device.client.constant.YG;
import com.bbzn.device.client.dto.ct.request.RequestCreateGroup;
import com.bbzn.device.client.dto.ct.request.RequestDeleteGroup;
import com.bbzn.device.client.dto.ct.request.RequestUptDevGrp;
import com.bbzn.device.client.dto.ct.request.RequestUptDevGrpRelation;
import com.bbzn.device.client.service.emq.DeviceGroupService;
import com.ctg.ag.sdk.biz.AepDeviceGroupManagementClient;
import com.ctg.ag.sdk.biz.aep_device_group_management.*;
import org.springframework.stereotype.Service;

/**
 * @Author wulongbo
 * @Date 2021/9/2 15:33
 * @Version 1.0
 */
@Service
public class DeviceGroupServiceImpl implements DeviceGroupService {


    @Override
    public CreateDeviceGroupResponse createDeviceGroup(String masterKey, RequestCreateGroup requestGroup) throws Exception {
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestGroup).getBytes();
        AepDeviceGroupManagementClient client = AepDeviceGroupManagementClient.newClient()
                .appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        CreateDeviceGroupRequest request = new CreateDeviceGroupRequest();
        request.setParamMasterKey(masterKey);    // single value
        request.setBody(body);    //具体格式见前面请求body说明
        CreateDeviceGroupResponse createDeviceGroupResponse = client.CreateDeviceGroup(request);
        System.out.println(createDeviceGroupResponse);
        client.shutdown();
        return createDeviceGroupResponse;
    }

    @Override
    public DeleteDeviceGroupResponse deviceGroup(String masterKey, RequestDeleteGroup requestDeleteGroup) throws Exception {
        AepDeviceGroupManagementClient client = AepDeviceGroupManagementClient.newClient()
                .appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        DeleteDeviceGroupRequest request = new DeleteDeviceGroupRequest();
        // set your request params here
        request.setParamProductId(requestDeleteGroup.getProductId());    // single value
        request.setParamDeviceGroupId(requestDeleteGroup.getDeviceGroupId());    // single value
        request.setParamMasterKey(masterKey);    // single value
        DeleteDeviceGroupResponse deleteDeviceGroupResponse = client.DeleteDeviceGroup(request);
        System.out.println(deleteDeviceGroupResponse);
        client.shutdown();
        return deleteDeviceGroupResponse;
    }

    @Override
    public UpdateDeviceGroupResponse updateDeviceGroup(String masterKey, RequestUptDevGrp requestUptDevGrp) throws Exception {
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestUptDevGrp).getBytes();
        AepDeviceGroupManagementClient client = AepDeviceGroupManagementClient.newClient()
                .appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        UpdateDeviceGroupRequest request = new UpdateDeviceGroupRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);    // single value
        request.setBody(body);    //具体格式见前面请求body说明
        UpdateDeviceGroupResponse updateDeviceGroupResponse = client.UpdateDeviceGroup(request);
        System.out.println(updateDeviceGroupResponse);
        client.shutdown();
        return updateDeviceGroupResponse;
    }

    @Override
    public UpdateDeviceGroupRelationResponse updateDeviceGroupRelation(String masterKey, RequestUptDevGrpRelation requestUptDevGrpRelation) throws Exception {
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestUptDevGrpRelation).getBytes();
        AepDeviceGroupManagementClient client = AepDeviceGroupManagementClient.newClient()
                .appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        UpdateDeviceGroupRelationRequest request = new UpdateDeviceGroupRelationRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);    // single value
        request.setBody(body);    //具体格式见前面请求body说明
        UpdateDeviceGroupRelationResponse updateDeviceGroupRelationResponse = client.UpdateDeviceGroupRelation(request);
        System.out.println(updateDeviceGroupRelationResponse);
        client.shutdown();
        return updateDeviceGroupRelationResponse;
    }
}
